package javaCode.chapter13.collection_;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author 徐州城
 * @version 1.0
 */
public class CollectionExercise {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        Dog d1 = new Dog("大黄", 1);
        Dog d2 = new Dog("小白", 2);
        Dog d3 = new Dog("小灰", 3);
        List col = new ArrayList();
        col.add(d1);
        col.add(d2);
        col.add(d3);

        Iterator iterator = col.iterator();
        System.out.println("===迭代器");
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println("obj=" + obj);
        }

        System.out.println("===增强for");
        for (Object obj :col) {
            System.out.println("obj=" + obj);
        }


    }
}

/*
请编写程序
1. 创建 3个 Dog{name, age} 对象，放入到 ArrayList 中，赋给 List 引用
2. 用迭代器和增强 for 循环两种方式来遍历
3. 重写Dog 的toString方法，输出 name和age
 */
class Dog{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}